<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/addRowShareFunction.js" ></script>
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div style="width: 100%;">
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" method="post">
				<table id="basicInfo" style="margin-left: 20%;padding: 20px 0px;">
					<tr>
						<td style="display: none;"><input type="text" name="TENANT_ID" id="input_TENANT_ID" /></td>
						<td width="12%">商户姓名</td>
						<td width="21%">
							<input class="easyui-combobox" type="text" id="input_TENANT_NAME" data-options="required:true" name="TENANT_NAME" style="width:100%"></input>
						</td>
						<td width="12%">身份证号</td>
						<td width="21%">
							<input class="easyui-textbox" name="IDCARD" id="input_IDCARD" style="width:100%"></input>
						</td>
						<td width="12%">收费单据编号</td>
						<td width="21%"><input class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" style="width:100%" data-options="prompt:'由系统自动生成',disabled:true"></input>
						</td>
					</tr>
					<tr style="display: none;">
						<td width="12%">状态</td>
						<td width="21%"><input class="easyui-textbox" id="input_STATE" name="STATE" style="width:100%" data-options="prompt:'由系统自动生成',disabled:true"></input>
						</td>
						<td width="12%">ID</td>
						<td width="21%"><input class="easyui-textbox" id="input_ID" name="ID" style="width:100%" data-options="prompt:'由系统自动生成',disabled:true"></input>
						</td>
					</tr>
				</table>
				<table id="tt" style="width: 100%;">
				</table>
				<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" id="input_COST" style="border: none;" id="xx" name="sum" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
				<div id="new_footer" style="margin-left: 23%;">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var currRow;
		var isVerify;
		var updateData = {};
		updateData.rows = [];
		var MinPage = 1;
		var typeInfo = new Array(); //将typeInfo初始化为数组
		var _detailToolbarData;
		var AddDialog;
		_detailToolbarData = AddDetailToolBar(7);
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
		});
		if(id == undefined) {
			currid = AddFlag;
		}
		///增加脚注
		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);

		getPageMaxAndMin();
		addFooter($("#new_footer"));

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/tenant/getmaxminid.do";
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		GetTable();
		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						currid = result.ID; //表示传回的ID
						//						currid = result.TENANT_ID;\
						currPage = result.ID;
						updateData=deepCopy(result);
						isVerify = result.ISVERIFY;
						verifyStatus(result.ISVERIFY, true);
						$('#ff').form('load', result);
						$("#tt").datagrid('loadData', result.rows);
						CalSum();
					}
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		//导出
		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/market/charge/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		function frontpage() {
			if(currPage >= MinPage) {
				var _url = RemoteLinkAddress + '/market/charge/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() {
			if(currPage <= MaxPage) {
				var _url = RemoteLinkAddress + '/market/charge/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function newdata() {
			$("#notreview").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			currPage = 0;
			updateData = {};
			$('#ff').form('reset');
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
			verifyStatus(isVerify, false);
		}

		function save() {
			if(isVerify == 1) {
				$.messager.alert("警告", "订单已审核，无法修改！");
				return;
			}
			getData();
		}

		function getData() {
			var data = new Object();
			data = submitFormData();
			data.COST = $("#input_COST").val();
			var TENANT_ID = $("input[name='TENANT_ID']").val();
			if(TENANT_ID) {
				data.TENANT_ID = TENANT_ID;
				data.TENANT_TYPE = 0; //市场人员
			} else {
				data.TENANT_TYPE = 1; //非市场人员
			}
			for(s in data)
			{
				updateData[s]=data[s];
			}
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "数据填写不完整");
				return;
			}
			var dataGridRows = $("#tt").datagrid('getRows');
			if(dataGridRows.length == 0){
				$.messager.alert("警告","明细信息不能为空");
				return ;
			}
			if(currid != AddFlag) {
				submitData(updateData);
			} else {
				data.rows = dataGridRows;
				submitData(data);
			}
		}
		
		function checkDetailComplete(_data){
			for(var s in _data){
				if(_data[s] === "" || _data[s] == undefined){
					return false;
				}
			}
			return true;
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/market/charge/save.do";
			} else {
				_url = RemoteLinkAddress + "/market/charge/update.do";
			}
			var sendData = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + sendData,
				type: 'post',
				success: function(result) {
					var revData = result.msg.split(";");
					if(revData[0] != "success" || result == null){
						$.messager.alert('提示信息', '保存失败!');
						return ;
					}
					if(currid == AddFlag) {
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						currid=revData[1];
						currPage=revData[1];
						$(document).ready(function(){
							loadRemote();
						});
						$.messager.alert("提示", "新建保存成功");
						return;
					} 
					$.messager.alert('提示信息', '保存成功!');
				},error:function(result){
					$.messager.alert('提示信息', '保存失败!');
				}
			});
		}

		function print() {
			var pLocation = "";
			if(isVerify == 0) {
				$.messager.alert("警告", "订单未审核，不能打印！");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("错误", "当前页面为新增页面不能打印");
				return;
			} else {
				pLocation = "receiptPrint.html?id=" + currid;
				window.open(pLocation);
			}
		}

		function searchmore() {
			AddTabIn("收费查询", parent.$('#tabs'), "UI/Market/Receipt/receiptSearch.html");
		}

		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/market/charge/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function GetTable() {
			$('#tt').edatagrid({
				height: 350,
				toolbar: _detailToolbarData,
				pagination: 'true',
				rownumbers: 'true',
				singleSelect: true,
				fitColumns: true,
				autoSave: 'true',
				columns: [
					[{
							field: 'ID',
							title: 'ID',
							width: '0%',
							align: 'center'
						}, {
							field: 'ORDER_ID',
							title: 'ORDER_ID',
							width: '0%',
							align: 'center'
						}, {
							field: 'CHARGETYPE_NAME',
							title: '收费类型',
							width: '20%',
							align: 'center',
							editor: {
								type: 'combobox',
								options: {
									url: RemoteLinkAddress + "/market/chargetype/listall.do",
									valueField: "NAME",
									textField: "NAME",
									required: true,
								}
							}
						}, {
							field: 'COST',
							title: '费用',
							width: '19%',
							align: 'center',
							editor: {
								type: 'numberbox',
								options: {
									valueField: "COST",
									textField: "COST",
								}
							},
							formatter: function(value, row, index) {
								return parseInt(value);
							}
						}, {
							field: 'STARTTIME',
							title: '起始时间',
							width: '19%',
							align: 'center',
							editor: {
								type: 'datebox',
								options: {
									valueField: "STARTTIME",
									textField: "STARTTIME",
								}
							}
						}, {
							field: 'ENDTIME',
							title: '结束时间',
							width: '19%',
							align: 'center',
							editor: {
								type: 'datebox',
								options: {
									valueField: "ENDTIME",
									textField: "ENDTIME"
								}
							}
						},
						{
							field: 'PERIOD',
							title: '时间期限',
							width: '19%',
							align: 'center',
							editor: {
								type: 'textbox',
								options: {
									valueField: "PERIOD",
									textField: "PERIOD",
									editable: false
								}
							}

						}
					]
				],
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				},
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					
					var result = CalTimeLimit(row.STARTTIME, row.ENDTIME, TIME_DAY); //将上述两个的值相乘再复制给result
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							PERIOD: result
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					CalSum();
					if(currid != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											obj[s] = row[s];
										}
									}
								}
							}
						} else {
							var len = updateData.rows.length;
							updateData.rows[len] = row;
							updateData.rows[len].ORDER_ID = currid;
						}
					}
				},
				onDestroy:function(index,row) {
					CalSum();
					if(currid != AddFlag) {
						for(var i = 0; i < updateData.rows.length; i++) {
							if(updateData.rows[i].ID == row.ID) {
								updateData.rows[i].ORDER_ID = ""; //直接替换
							}
						}
					}
				}
			});

			$('#tt').datagrid('hideColumn', 'ID');
			$('#tt').datagrid('hideColumn', 'ORDER_ID');

		}

		function AddDict() {
			var pLocation = "../DictData/DictDataAdd.html?id=region";
			$("#ifrmEdit").attr("src", pLocation);
			constructcomboDialog($("#dlg"), "添加基础信息", $("#input_REGION"));
			AddDialog.dialog('open');
		}

		$('#input_TENANT_NAME').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/market/tenant/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) {
				$("#input_TENANT_ID").val(record.ID);
				$("#input_IDCARD").textbox('setValue', record.IDCARD);
			}
		});

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				if(dataGridRows[i].COST != undefined) {
					if(dataGridRows[i].COST === ""){
						dataGridRows[i].COST = 0;
					}
					_num += parseInt(dataGridRows[i].COST);
				}
			}
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
			$("#input_COST").val(_num);
		}

		function deletedata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "订单已审核，无法删除");
				return;
			}
			$.messager.confirm("确认", "是否删除", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/charge/delete.do", {
						ID: currid
					}, function(result) {
						if(result.msg == "success") {
							nextpage();
							$.messager.alert({
								title: '成功',
								msg: '删除成功!'
							});
						} else {
							$.messager.alert({
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}

		function verifydata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "订单已审核！");
				return;
			}
			$.messager.confirm("确认", "是否审核", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/charge/verify.do", {
						"ID": currid,
					}, function(result) {
						if(result.msg == "success") {
							$.messager.alert({
								title: '成功',
								msg: '审核成功!'
							});
							verifyStatus(1, true);
							isVerify = 1;
						} else {
							$.messager.alert({
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}

		function unverifydata() {
			if(isVerify == 0) {
				$.messager.alert("警告", "订单已弃审！");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/charge/unverify.do", {
							"ID": currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '放弃审核成功!'
								});
								verifyStatus(0, true);
								isVerify = 0;
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}
	
	</script>

</html>